package cn.xd.learn.itstack.dbatis.spring;

import cn.xd.learn.itstack.dbatis.config.DefaultSqlSessionFactory;
import cn.xd.learn.itstack.dbatis.config.SqlSession;
import cn.xd.learn.itstack.dbatis.config.SqlSessionFactory;
import cn.xd.learn.itstack.dbatis.config.SqlSessionFactoryBuilder;
import cn.xd.learn.itstack.dbatis.utils.Resources;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

import java.io.Reader;

/**
 * @Classname SqlSessionFactoryBean
 * @Description TODO
 * @Date 2022/4/26 下午2:44
 * @Created by lvxiaodong
 */
public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean {

    private String resource;

    private SqlSessionFactory sqlSessionFactory;


    @Override
    public void afterPropertiesSet() throws Exception {
       Reader reader = Resources.getResourceAsReader(resource);
       if(reader!=null){
           this.sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
       }

    }

    @Override
    public SqlSessionFactory getObject() throws Exception {
        return sqlSessionFactory;
    }

    @Override
    public Class<?> getObjectType() {
        return SqlSessionFactory.class;
    }

    public void setResource(String resource) {
        this.resource = resource;
    }
}
